package com.example.writting.ui.documents

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.fragment.app.Fragment
import com.example.writting.databinding.FragmentDocumentsBinding

class DocumentsFragment : Fragment() {

    private var _binding: FragmentDocumentsBinding? = null
    private val binding get() = _binding!!

    private val types = listOf("法定公文", "常用文书", "礼仪文书")

    private val formats = mapOf(
        "法定公文" to listOf(
            "命令", "决定", "公告", "通告", "通知", "通报", "议案", "报告",
            "请示", "批复", "意见", "函", "纪要", "决议", "公报"
        ),
        "常用文书" to listOf(
            "工作计划", "工作简报", "工作总结", "工作要点", "方案", "调查报告",
            "会议开幕词", "会议主持词", "会议讲话", "会议闭幕词", "消息", "行政问责类文书"
        ),
        "礼仪文书" to listOf(
            "慰问信", "表扬信", "邀请函", "感谢信", "贺信", "祝词",
            "倡议书", "讣告", "唁函"
        )
    )

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentDocumentsBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 设置类型下拉列表
        val typeAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, types)
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.typeSpinner.adapter = typeAdapter

        // 初始时禁用格式选择器
        binding.formatSpinner.isEnabled = false

        // 设置类型选择监听器
        binding.typeSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                // 当选择了类型时，更新格式选项并启用格式选择器
                val selectedType = types[position]
                val formatList = formats[selectedType] ?: emptyList()
                val formatAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, formatList)
                formatAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                binding.formatSpinner.adapter = formatAdapter
                binding.formatSpinner.isEnabled = true
                updateResult()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                binding.formatSpinner.isEnabled = false
                binding.resultText.text = ""
            }
        }

        // 设置格式选择监听器
        binding.formatSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                updateResult()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                binding.resultText.text = ""
            }
        }
    }

    private fun updateResult() {
        binding.resultText.text = "暂无"
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
} 